package com.lcxw.hxzbadmin.configcontroller;

import com.alibaba.fastjson.JSONObject;
import java.util.List;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzbadmin.entity.configbean.ServerConfig;
import com.lcxw.hxzbadmin.service.configservice.ServerConfigService;
import com.lcxw.hxzbapi.entity.response.Response;
import com.lcxw.hxzbcommon.common.StringUtil;

/**
 * 服务器配置
 */

@RestController
@RequestMapping("/admin/server")
@CrossOrigin
public class ServerConfigController {
    private Logger logger = LoggerFactory.getLogger(ServerConfigController.class);

    @Resource
    ServerConfigService serverConfigService;

    @PostMapping("/add")
    public Response addServer(@RequestBody JSONObject requestParams){
        logger.info("admin {} add a new server",requestParams.getString("adminName"));

        ServerConfig serverConfig = new ServerConfig();

        serverConfig.setName(requestParams.getString("name"));
        serverConfig.setIp(requestParams.getString("ip"));
        serverConfig.setIsDefault(requestParams.getInteger("isDefault"));
        serverConfig.setStatus(1);
        serverConfig.setAddTime(StringUtil.getDateTime());

        try {
            serverConfigService.insert(serverConfig);
        } catch (Exception e) {
            return Response.dbFail(logger,"insert server");
        }
        return Response.ok();

    }

    @PostMapping("/delete")
    public Response deleteServer(@RequestBody JSONObject requestParams){
        logger.info("admin {} add a new server,server ID:{}",requestParams.getString("adminName"),requestParams.getInteger("id"));

        int id = requestParams.getInteger("id");

        try {
            serverConfigService.deleteByPrimaryKey(id);
        } catch (Exception e) {
            return Response.dbFail(logger,"delete server");
        }

        return Response.ok();
    }

    @PostMapping("/getAll")
    public Response getAllServer(@RequestBody JSONObject requestParams){
        List<ServerConfig> data;

        try {
            data = serverConfigService.selectAll();
        } catch (Exception e) {
            return Response.dbFail(logger,"select all server");
        }

        return Response.ok(data);
    }

    @PostMapping("/mod")
    public Response modServer(@RequestBody JSONObject requestParams){

        ServerConfig serverConfig = new ServerConfig();

        serverConfig.setId(requestParams.getInteger("id"));
        serverConfig.setName(requestParams.getString("name"));
        serverConfig.setIp(requestParams.getString("ip"));
        serverConfig.setIsDefault(requestParams.getInteger("isDefault"));
        serverConfig.setStatus(requestParams.getInteger("status"));

        try {
            serverConfigService.updateByPrimaryKey(serverConfig);
        } catch (Exception e) {
            return Response.dbFail(logger,"update server");
        }

        return Response.ok();


    }

}
